<?php
namespace yan\editor_frontend\editorxml;
use yii\helpers\Html;

class WebSliderWidget extends WebWidget{
	public function run(){
		SliderAsset::register($this->view);
		$options = $this->options;
		$shtml = Html::beginTag('div', $options);
		$shtml .= '<div class="swiper" id="'.$this->id.'">';
		$shtml .= '<div class="swiper-wrapper">';

		foreach ((array)$this->value['value']['content']['path'] as $key => $val) {
			$option = isset($this->value['value']['content']['options'][$key]) ? $this->value['value']['content']['options'][$key] : '';
			$shtml .= '<div class="swiper-slide item">'.Html::img( $val ). Html::tag('div', $option, ['class'=>'option']) .'</div>';
		}
		$shtml .= '</div>';
		$shtml .= '<div class="swiper-button-next"></div>
      				<div class="swiper-button-prev"></div>';
		$shtml .= '<div class="swiper-pagination"></div>';
		$shtml .= '</div>';
		$shtml .= Html::endTag('div');
		$this->registerJs();

		return $shtml;
	}

	public function registerJs(){
		$js = '
			var swiper = new Swiper("#'.$this->id.'", {
		        navigation: {
		          nextEl: ".swiper-button-next",
		          prevEl: ".swiper-button-prev",
		        },
		        pagination: {
		          el: ".swiper-pagination",
		        },
		      });
		';
		$this->view->registerJs($js);
	}
}